package swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class AutomaticTellerService implements ActionListener {
	public Box getBv() {
		return bv;
	}

	public JPanel getEastPanel() {
		return eastPanel;
	}

	private String password = "";
	private JPasswordField jPasswordField;
	private JButton[] arrayBtn = null;

	public void setPassword(String password) {
		this.password = password;
	}

	private Box bv;
	private JPanel eastPanel;

	public AutomaticTellerService() {

		setGUI();
	}

	public void setGUI() {
		JFrame frame = new JFrame("Automatic teller service");
		eastPanel = new JPanel();
		JPanel centerPanel = new JPanel();
		JPanel jp = new JPanel();
		jPasswordField = new JPasswordField(10);
		jPasswordField.setBackground(Color.WHITE);
		jPasswordField.setEditable(false);

		jp.setLayout(new BorderLayout(10, 10));
		createPressButtons(centerPanel);

		// Create box of components
		bv = Box.createVerticalBox();
		bv.setBorder(BorderFactory.createRaisedBevelBorder());
		bv.add(new JLabel("Enter password"));
		bv.add(jPasswordField);
		eastPanel.add(bv);
		eastPanel.setBorder(BorderFactory.createRaisedBevelBorder());
		jp.add(centerPanel, BorderLayout.CENTER);
		jp.add(eastPanel, BorderLayout.EAST);

		frame.add(jp);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(new Dimension(500, 300));

		frame.setLocationRelativeTo(null);
		frame.setVisible(true);

	}

	public void createPressButtons(JPanel centerPanel) {
		// set Jbutton
		arrayBtn = new JButton[12];
		arrayBtn[0] = new JButton("0");
		arrayBtn[1] = new JButton("1");
		arrayBtn[2] = new JButton("2");
		arrayBtn[3] = new JButton("3");
		arrayBtn[4] = new JButton("4");
		arrayBtn[5] = new JButton("5");
		arrayBtn[6] = new JButton("6");
		arrayBtn[7] = new JButton("7");
		arrayBtn[8] = new JButton("8");
		arrayBtn[9] = new JButton("9");
		arrayBtn[10] = new JButton("next");
		arrayBtn[11] = new JButton("CE");

		// it take us possibility to use button by pressing Alt+ (special
		// number) on keyboard
		arrayBtn[0].setMnemonic(KeyEvent.VK_0);
		arrayBtn[1].setMnemonic(KeyEvent.VK_1);
		arrayBtn[2].setMnemonic(KeyEvent.VK_2);
		arrayBtn[3].setMnemonic(KeyEvent.VK_3);
		arrayBtn[4].setMnemonic(KeyEvent.VK_4);
		arrayBtn[5].setMnemonic(KeyEvent.VK_5);
		arrayBtn[6].setMnemonic(KeyEvent.VK_6);
		arrayBtn[7].setMnemonic(KeyEvent.VK_7);
		arrayBtn[8].setMnemonic(KeyEvent.VK_8);
		arrayBtn[9].setMnemonic(KeyEvent.VK_9);
		arrayBtn[10].setMnemonic(KeyEvent.VK_N);
		arrayBtn[11].setMnemonic(KeyEvent.VK_C);

		centerPanel.setLayout(new GridLayout(4, 3));
		for (int i = 0; i < arrayBtn.length; i++) {
			arrayBtn[i].addActionListener(this);
			centerPanel.add(arrayBtn[i]);
		}

	}

	@Override
	public void actionPerformed(ActionEvent ac) {
		String action = ac.getActionCommand();

		if (action.equals("next")) {
			proccesCheckingPass();
		}

		if (action.equals("CE")) {
			int lastPosition;
			if (password.length() > 0) {
				if (bv.getComponent(1) instanceof JPasswordField) {
					password = jPasswordField.getText();
					lastPosition = password.length() - 1;
				} else {
					JTextField textField = (JTextField) bv.getComponent(1);
					password = textField.getText();
					lastPosition = password.length() - 1;
				}

				removeCharAt(password, lastPosition);
			}

		}
		// it take us possibility write it TextField by pressing on Buttons
		for (int i = 0; i < arrayBtn.length - 2; i++) {
			if (action.equals(arrayBtn[i].getText())) {
				password += "" + arrayBtn[i].getText();
				if (bv.getComponent(1) instanceof JPasswordField) {
					jPasswordField.setText(password);
				} else {
					JTextField textField = (JTextField) bv.getComponent(1);
					textField.setText(password);
				}

			}
		}

	}

	// it remove last character from string
	public void removeCharAt(String str, int pos) {
		if (str.length() > 0) {
			if (bv.getComponent(1) instanceof JPasswordField) {
				jPasswordField.setText(str.substring(0, pos)
						+ str.substring(pos + 1));
			} else {
				JTextField textField = (JTextField) bv.getComponent(1);
				textField.setText(str.substring(0, pos)
						+ str.substring(pos + 1));
			}
		}
	}

	// This function check if user have accesses enter in ATS
	public boolean checkPassword() {

		return true;
	}

	public String getPassword() {
		return password;
	}

	public void errorPassword(String text) {
		JOptionPane.showMessageDialog(null, text, "Invalid password",
				JOptionPane.ERROR_MESSAGE);
		jPasswordField.setText("");
	}

	public void proccesCheckingPass() {
		password = jPasswordField.getText();
		System.out.println("Password recived : " + getPassword());
		if (!password.equals("")) {
			if (checkPassword() == true) {
				new UserMenuManagment(this);
				arrayBtn[10].setEnabled(false);
				System.out.println("Entred in system");
			} else {
				errorPassword("Please check your password");
			}
		} else {
			errorPassword("You didn't enter nothing");
		}
	}

}
